/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.core.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.losenote.core.MessageComponent;
import com.losenote.enums.MessageTypeEnum;
import com.losenote.model.Message;
import com.losenote.model.OperateContext;
import com.losenote.model.ct.MessageConvertor;
import com.losenote.orm.MessageDO;
import com.losenote.orm.MessageDODAO;
import com.losenote.orm.MessageDOExample;

/**
 * 消息组件
 * 
 * @author danjingyu
 * @version $Id: MessageComponentImpl.java, v 0.1 2013-12-1 下午4:39:25 danjingyu Exp $
 */
public class MessageComponentImpl implements MessageComponent {

    @Autowired
    private MessageDODAO messageDODAO;

    /** 
     * @see com.losenote.core.MessageComponent#saveMessage(com.losenote.model.Message)
     */
    public int saveMessage(MessageDO messageDO, OperateContext context) {
        int id = messageDODAO.insert(messageDO);
        return id;
    }

    /**
     * 查询消息
     * 
     * @see com.losenote.core.MessageComponent#queryRecentMessage(int, com.losenote.enums.MessageTypeEnum)
     */
    @SuppressWarnings("unchecked")
    public List<Message> queryMessages(int themeId, MessageTypeEnum type) {
        MessageDOExample example = new MessageDOExample();
        example.createCriteria().andRelIdEqualTo(themeId).andRelTypeEqualTo(type.getValue());
        List<MessageDO> questionD0s = messageDODAO.selectByExample(example);
        List<Message> messages = new ArrayList<Message>();
        if (CollectionUtils.isEmpty(questionD0s)) {
            return messages;
        }
        for (MessageDO messageDO : questionD0s) {
            messages.add(MessageConvertor.fromDO(messageDO));
        }
        return messages;
    }

    public void setMessageDODAO(MessageDODAO messageDODAO) {
        this.messageDODAO = messageDODAO;
    }

}
